package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.BydspecialPoject;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.BydspecialPojectMapper;
import com.ruoyi.system.service.BydspecialPojectService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class BydspecialPojectServiceIpml implements BydspecialPojectService {
    @Autowired
    private BydspecialPojectMapper bydspecialPojectMapper;

    @Override
    public R insertData(InsertBydSpecialPojectDto insertBydSpecialPojectDto) {
        if (insertBydSpecialPojectDto == null){
            return R.fail("请填写数据后保存");
        }
       int sum =  bydspecialPojectMapper.insertData(insertBydSpecialPojectDto);
       if (sum >= 1){
           return R.ok("保存成功");
       }
        return R.fail("保存失败，请联系管理员处理");
    }

    @Override
    public R upadteData(UpdateBydSpecialPojectDto updateBydSpecialPojectDto) {
        if (updateBydSpecialPojectDto == null){
            return R.fail("请正确填写数据");
        }
        int sum = bydspecialPojectMapper.updateData(updateBydSpecialPojectDto);
        if (sum >= 1){
            return R.ok("更改成功");
        }
        return R.fail("更改数据失败，请联系管理员处理");
    }

    @Override
    public List<SelectBydSpecialPojectDto> selectList(BydSpecialPojectDto specialPojectDto) {
        List<SelectBydSpecialPojectDto> insertBydSpecialPojectDto = new ArrayList<>();
        List<BydspecialPoject> pojects = bydspecialPojectMapper.selectBydspecialPojectList(specialPojectDto);
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (BydspecialPoject byd: pojects) {
                SelectBydSpecialPojectDto bydSpecialPojectDto = new SelectBydSpecialPojectDto();
                BeanUtils.copyProperties(byd,bydSpecialPojectDto);
            if (byd.getShippingDate() != null){
                String shippingDate = sdf.format(byd.getShippingDate());
                bydSpecialPojectDto.setShippingDate(shippingDate);
                }
                String order = sdf.format(byd.getOrderDate());
                String delivery = sdf.format(byd.getDeliveryDate());
                if (byd.getChangeDate() !=null && byd.getStorageTime() != null){
                    String changeDate = sdf.format(byd.getChangeDate());
                    String storageTime = sdf.format(byd.getStorageTime());
                    bydSpecialPojectDto.setChangeDate(changeDate);
                    bydSpecialPojectDto.setStorageTime(storageTime);
                }
                bydSpecialPojectDto.setOrderDate(order);
                bydSpecialPojectDto.setDeliveryDate(delivery);


                insertBydSpecialPojectDto.add(bydSpecialPojectDto);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return insertBydSpecialPojectDto;
    }

//    public List<PageTableData> extractTables(MultipartFile file) {
//        List<PageTableData> result = new ArrayList<>();
//
//        try (PDDocument document = PDDocument.load(file.getInputStream())) {
//            int pageCount = document.getNumberOfPages();
//
//            for (int pageNum = 0; pageNum < pageCount; pageNum++) {
//                PDFTextStripper stripper = new PDFTextStripper();
//                stripper.setStartPage(pageNum + 1);
//                stripper.setEndPage(pageNum + 1);
//                String pageText = stripper.getText(document);
//
//                List<List<List<String>>> tables = extractTables(pageText);
//                if (!tables.isEmpty()) {
//                    PageTableData pageData = new PageTableData();
//                    pageData.setPageNumber(pageNum + 1);
//                    pageData.setTables(tables);
//                    result.add(pageData);
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }

    @Override
    public List<String> selectModelByOrder(String customerOrder) {
        List<String> modelName = bydspecialPojectMapper.selectModelByOrder(customerOrder);
        List<String> re = new ArrayList<>();
        for (String name:modelName) {
            String[] parts = name.split("-", 2); // 最多分割成两部分
            String result = parts[0];
            re.add(result);
        }
        return re;
    }

    private List<List<List<String>>> extractTables(String pageText) {
        // 表格提取逻辑与之前相同...
        List<List<List<String>>> allTables = new ArrayList<>();
        String[] lines = pageText.split("\n");

        Pattern tableStartPattern = Pattern.compile("^[-+|=]{5,}.*$");
        Pattern tableEndPattern = Pattern.compile("^[-+|=]{5,}.*$");

        boolean inTable = false;
        List<List<String>> currentTable = new ArrayList<>();

        for (String line : lines) {
            if (tableStartPattern.matcher(line).find()) {
                inTable = true;
                continue;
            }

            if (tableEndPattern.matcher(line).find() && inTable) {
                inTable = false;
                if (!currentTable.isEmpty()) {
                    allTables.add(currentTable);
                    currentTable = new ArrayList<>();
                }
                continue;
            }

            if (inTable && !line.trim().isEmpty()) {
                String[] cells = line.split("\\|");
                if (cells.length <= 1) {
                    cells = line.split("\\s{2,}");
                }

                List<String> row = new ArrayList<>();
                for (String cell : cells) {
                    row.add(cell.trim());
                }
                currentTable.add(row);
            }
        }

        if (!currentTable.isEmpty()) {
            allTables.add(currentTable);
        }

        return allTables;
    }
}
